package com.yunkaweilai.android.activity.member;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.lazylibrary.util.StringUtils;
import com.google.gson.Gson;
import com.luck.picture.lib.tools.PictureFileUtils;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.base.BaseApplication;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.model.ImageUploadModel;
import com.yunkaweilai.android.model.MemberAlbumModel;
import com.yunkaweilai.android.net.Glide.GlideUtils;
import com.yunkaweilai.android.utils.PicturePickerHelper;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 会员相册
 * Created by Administrator on 2017/9/18.
 */

public class MemberAlbumActivity extends BaseActivity {

    @BindView(R.id.id_img_head_1)
    ImageView idImgHead1;
    @BindView(R.id.id_edt_1)
    EditText idEdt1;
    @BindView(R.id.id_tv_clean_1)
    TextView idTvClean1;
    @BindView(R.id.id_img_head_2)
    ImageView idImgHead2;
    @BindView(R.id.id_edt_2)
    EditText idEdt2;
    @BindView(R.id.id_tv_clean_2)
    TextView idTvClean2;
    @BindView(R.id.id_img_head_3)
    ImageView idImgHead3;
    @BindView(R.id.id_edt_3)
    EditText idEdt3;
    @BindView(R.id.id_tv_clean_3)
    TextView idTvClean3;
    @BindView(R.id.id_img_head_4)
    ImageView idImgHead4;
    @BindView(R.id.id_edt_4)
    EditText idEdt4;
    @BindView(R.id.id_tv_clean_4)
    TextView idTvClean4;
    @BindView(R.id.id_img_head_5)
    ImageView idImgHead5;
    @BindView(R.id.id_edt_5)
    EditText idEdt5;
    @BindView(R.id.id_tv_clean_5)
    TextView idTvClean5;
    @BindView(R.id.id_img_head_6)
    ImageView idImgHead6;
    @BindView(R.id.id_edt_6)
    EditText idEdt6;
    @BindView(R.id.id_tv_clean_6)
    TextView idTvClean6;
    public static final String ALBUM_LIST_GET_LIST = "ALBUM_LIST_GET_LIST";
    public static final String ALBUM_LIST = "ALBUM_LIST";
    private ArrayList<MemberAlbumModel> getlist = new ArrayList<>();
    private String album_img_1 = "", album_img_2 = "", album_img_3 = "", album_img_4 = "", album_img_5 = "", album_img_6 = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_member_album);
        ButterKnife.bind(this);
        new TitleBuilder(mAc).setTitleText("会员相册")
                .setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                }).setRightText("保存").setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveAlbum();
            }


        });
        getlist = (ArrayList<MemberAlbumModel>) getIntent().getSerializableExtra(ALBUM_LIST_GET_LIST);
        if (getlist == null) {
            getlist = new ArrayList<>();
        }
        initView();

    }

    private void initView() {
        for (int i = 0; i < getlist.size(); i++) {
            MemberAlbumModel memberAlbumModel = getlist.get(i);
        }

        for (int i = 0; i < getlist.size(); i++) {
            MemberAlbumModel memberAlbumModel = getlist.get(i);
            if ("1".equals(memberAlbumModel.getId())) {
                idEdt1.setText(memberAlbumModel.getAlbum_remark() + "");
                GlideUtils.loadImage(mAc, memberAlbumModel.getPath(), idImgHead1);
                album_img_1 = memberAlbumModel.getPath();
            } else if ("2".equals(memberAlbumModel.getId())) {
                idEdt2.setText(memberAlbumModel.getAlbum_remark() + "");
                GlideUtils.loadImage(mAc, memberAlbumModel.getPath(), idImgHead2);
                album_img_2 = memberAlbumModel.getPath();
            } else if ("3".equals(memberAlbumModel.getId())) {
                idEdt3.setText(memberAlbumModel.getAlbum_remark() + "");
                GlideUtils.loadImage(mAc, memberAlbumModel.getPath(), idImgHead3);
                album_img_3 = memberAlbumModel.getPath();
            } else if ("4".equals(memberAlbumModel.getId())) {
                idEdt4.setText(memberAlbumModel.getAlbum_remark() + "");
                GlideUtils.loadImage(mAc, memberAlbumModel.getPath(), idImgHead4);
                album_img_4 = memberAlbumModel.getPath();
            } else if ("5".equals(memberAlbumModel.getId())) {
                idEdt5.setText(memberAlbumModel.getAlbum_remark() + "");
                GlideUtils.loadImage(mAc, memberAlbumModel.getPath(), idImgHead5);
                album_img_5 = memberAlbumModel.getPath();
            } else if ("6".equals(memberAlbumModel.getId())) {
                idEdt6.setText(memberAlbumModel.getAlbum_remark() + "");
                GlideUtils.loadImage(mAc, memberAlbumModel.getPath(), idImgHead6);
                album_img_6 = memberAlbumModel.getPath();
            }
        }
    }

    //保存相册
    private void saveAlbum() {
        ArrayList<MemberAlbumModel> list = new ArrayList<>();
        MemberAlbumModel model;
        if (!StringUtils.isEmpty(album_img_1)) {
            model = new MemberAlbumModel();
            model.setId("1");
            model.setPath(album_img_1);
            model.setAlbum_remark(idEdt1.getText().toString() + "");
            list.add(model);
        }
        if (!StringUtils.isEmpty(album_img_2)) {
            model = new MemberAlbumModel();
            model.setId("2");
            model.setPath(album_img_2);
            model.setAlbum_remark(idEdt2.getText().toString() + "");
            list.add(model);
        }
        if (!StringUtils.isEmpty(album_img_3)) {
            model = new MemberAlbumModel();
            model.setId("3");
            model.setPath(album_img_3);
            model.setAlbum_remark(idEdt3.getText().toString() + "");
            list.add(model);
        }
        if (!StringUtils.isEmpty(album_img_4)) {
            model = new MemberAlbumModel();
            model.setId("4");
            model.setPath(album_img_4);
            model.setAlbum_remark(idEdt4.getText().toString() + "");
            list.add(model);
        }
        if (!StringUtils.isEmpty(album_img_5)) {
            model = new MemberAlbumModel();
            model.setId("5");
            model.setPath(album_img_5);
            model.setAlbum_remark(idEdt5.getText().toString() + "");
            list.add(model);
        }
        if (!StringUtils.isEmpty(album_img_6)) {
            model = new MemberAlbumModel();
            model.setId("6");
            model.setPath(album_img_6);
            model.setAlbum_remark(idEdt6.getText().toString() + "");
            list.add(model);
        }
        Intent intent = new Intent();
        intent.putExtra(ALBUM_LIST, (Serializable) list);
        setResult(Activity.RESULT_OK, intent);
        finish();

    }


    @OnClick({R.id.id_img_head_1, R.id.id_tv_clean_1, R.id.id_img_head_2, R.id.id_tv_clean_2, R.id.id_img_head_3, R.id.id_tv_clean_3, R.id.id_img_head_4, R.id.id_tv_clean_4, R.id.id_img_head_5, R.id.id_tv_clean_5, R.id.id_img_head_6, R.id.id_tv_clean_6})
    public void onViewClicked(View view) {
//        String cachePath = BoxingFileHelper.getCacheDir(this);
//        Uri destUri = new Uri.Builder()
//                .scheme("file")
//                .appendPath(cachePath)
//                .appendPath(String.format(Locale.US, "%s.jpg", System.currentTimeMillis()))
//                .build();
//        BoxingConfig singleCropImgConfig = new BoxingConfig(BoxingConfig.Mode.SINGLE_IMG).withCropOption(new BoxingCropOption(destUri).aspectRatio(1, 1)).needCamera();
        switch (view.getId()) {
            case R.id.id_img_head_1:
//                if (TextUtils.isEmpty(cachePath)) {
//                    Toast.makeText(this, R.string.storage_deny, Toast.LENGTH_SHORT).show();
//                    return;
//                }
//                Boxing.of(singleCropImgConfig).withIntent(this, PhotoPickerActivity.class).start(this, 1);
                            PicturePickerHelper.pickAvatar(this, 1);
                break;
            case R.id.id_tv_clean_1:
                album_img_1 = "";
                idEdt1.setText("");
                GlideUtils.loadImage(mAc, R.mipmap.ic_image_add, idImgHead1);
                break;
            case R.id.id_img_head_2:
//                if (TextUtils.isEmpty(cachePath)) {
//                    Toast.makeText(this, R.string.storage_deny, Toast.LENGTH_SHORT).show();
//                    return;
//                }
//                Boxing.of(singleCropImgConfig).withIntent(this, PhotoPickerActivity.class).start(this, 2);
                PicturePickerHelper.pickAvatar(this, 2);
                break;
            case R.id.id_tv_clean_2:
                album_img_2 = "";
                idEdt2.setText("");
                GlideUtils.loadImage(mAc, R.mipmap.ic_image_add, idImgHead2);
                break;
            case R.id.id_img_head_3:
//                if (TextUtils.isEmpty(cachePath)) {
//                    Toast.makeText(this, R.string.storage_deny, Toast.LENGTH_SHORT).show();
//                    return;
//                }
//                Boxing.of(singleCropImgConfig).withIntent(this, PhotoPickerActivity.class).start(this, 3);
                PicturePickerHelper.pickAvatar(this, 3);
                break;
            case R.id.id_tv_clean_3:
                album_img_3 = "";
                idEdt3.setText("");
                GlideUtils.loadImage(mAc, R.mipmap.ic_image_add, idImgHead3);
                break;
            case R.id.id_img_head_4:
//                if (TextUtils.isEmpty(cachePath)) {
//                    Toast.makeText(this, R.string.storage_deny, Toast.LENGTH_SHORT).show();
//                    return;
//                }
//                Boxing.of(singleCropImgConfig).withIntent(this, PhotoPickerActivity.class).start(this, 4);
                PicturePickerHelper.pickAvatar(this, 4);
                break;
            case R.id.id_tv_clean_4:
                album_img_4 = "";
                idEdt4.setText("");
                GlideUtils.loadImage(mAc, R.mipmap.ic_image_add, idImgHead4);
                break;
            case R.id.id_img_head_5:
//                if (TextUtils.isEmpty(cachePath)) {
//                    Toast.makeText(this, R.string.storage_deny, Toast.LENGTH_SHORT).show();
//                    return;
//                }
//                Boxing.of(singleCropImgConfig).withIntent(this, PhotoPickerActivity.class).start(this, 5);
                PicturePickerHelper.pickAvatar(this, 5);
                break;
            case R.id.id_tv_clean_5:
                album_img_5 = "";
                idEdt5.setText("");
                GlideUtils.loadImage(mAc, R.mipmap.ic_image_add, idImgHead5);
                break;
            case R.id.id_img_head_6:
//                if (TextUtils.isEmpty(cachePath)) {
//                    Toast.makeText(this, R.string.storage_deny, Toast.LENGTH_SHORT).show();
//                    return;
//                }
//                Boxing.of(singleCropImgConfig).withIntent(this, PhotoPickerActivity.class).start(this, 6);
                PicturePickerHelper.pickAvatar(this, 6);
                break;
            case R.id.id_tv_clean_6:
                album_img_6 = "";
                idEdt6.setText("");
                GlideUtils.loadImage(mAc, R.mipmap.ic_image_add, idImgHead6);
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (data != null) {
//                final ArrayList<BaseMedia> medias = Boxing.getResult(data);
//                BaseMedia baseMedia = medias.get(0);
//                if (!(baseMedia instanceof ImageMedia)) {
//                    return;
//                }
//                showProgressDialog("请稍等，图片上传中...");
//                final ImageMedia imageMedia = (ImageMedia) baseMedia;
//                //对返回的图片进行压缩
//                if (imageMedia.compress(new ImageCompressor(this))) {
//                    imageMedia.removeExif();
//                }
//                //压缩质量和尺寸 上面这个不靠谱
//                File fileDir = Environment.getExternalStorageDirectory();
//                BitmapFactory.Options opts = new BitmapFactory.Options();
//                Bitmap bitmap = BitmapFactory.decodeFile(imageMedia.getPath(), opts);// 文件路径
//                ImageUtilsK.compressImage2File(bitmap, new File(fileDir, "MemberAlbum.jpg"));
//                File file1 = new File(fileDir, "MemberAlbum.jpg");
//                uploadFile(file1.getAbsolutePath(), requestCode);
                String uri = PicturePickerHelper.onPickLogoOrAvatarActivityResult(requestCode, resultCode, data);
                if (!StringUtils.isEmpty(uri)) {
                    uploadFile(uri, requestCode);
                } else {
                    showToast("未选择照片");
                }

            }
        }
    }

    //文件上传
    private void uploadFile(final String mPicPath, final int type) {
        Map<String, String> paprms = new HashMap<>();
        paprms.put("token", BaseApplication.getToken());
        OkHttpUtils.post().addFile("file", "shop.jpg", new File(mPicPath))
                .url(Api.IMAGE_UPLOAD).params(paprms).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dismissProgressDialog();
                        showToast("上传失败");
                    }
                });
            }

            @Override
            public void onResponse(final String response, int id) {
                boolean isChecked = Utils.checkCode(mAc, response);
                final Gson gson = new Gson();
                if (!isChecked) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            dismissProgressDialog();
                        }
                    });
                } else {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (type == 1) {

                                dismissProgressDialog();
                                ImageUploadModel imageUploadModel = gson.fromJson(response, ImageUploadModel.class);
                                album_img_1 = imageUploadModel.getData().getPath();
                                GlideUtils.loadImage(mAc, album_img_1, idImgHead1);
                            } else if (type == 2) {
                                dismissProgressDialog();
                                ImageUploadModel imageUploadModel = gson.fromJson(response, ImageUploadModel.class);
                                album_img_2 = imageUploadModel.getData().getPath();
                                GlideUtils.loadImage(mAc, album_img_2, idImgHead2);
                            } else if (type == 3) {
                                dismissProgressDialog();
                                ImageUploadModel imageUploadModel = gson.fromJson(response, ImageUploadModel.class);
                                album_img_3 = imageUploadModel.getData().getPath();
                                GlideUtils.loadImage(mAc, album_img_3, idImgHead3);
                            } else if (type == 4) {
                                dismissProgressDialog();
                                ImageUploadModel imageUploadModel = gson.fromJson(response, ImageUploadModel.class);
                                album_img_4 = imageUploadModel.getData().getPath();
                                GlideUtils.loadImage(mAc, album_img_4, idImgHead4);
                            } else if (type == 5) {
                                dismissProgressDialog();
                                ImageUploadModel imageUploadModel = gson.fromJson(response, ImageUploadModel.class);
                                album_img_5 = imageUploadModel.getData().getPath();
                                GlideUtils.loadImage(mAc, album_img_5, idImgHead5);
                            } else if (type == 6) {
                                dismissProgressDialog();
                                ImageUploadModel imageUploadModel = gson.fromJson(response, ImageUploadModel.class);
                                album_img_6 = imageUploadModel.getData().getPath();
                                GlideUtils.loadImage(mAc, album_img_6, idImgHead6);
                            }
                            PictureFileUtils.deleteCacheDirFile(MemberAlbumActivity.this);//清除缓存
                        }
                    });
                }
            }
        });
    }
}
